package com.jtjrenren.android.taxi.passenger.core.order.history;

import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.jtjrenren.android.taxi.passenger.core.adapter.OrderHistoryAdapter;
import com.jtjrenren.android.taxi.passenger.Constants;
import com.jtjrenren.android.taxi.passenger.core.order.status.OrderStatusActivity;
import com.jtjrenren.android.taxi.passenger.core.pay.main.PayActivity;
import com.jtjrenren.android.taxi.passenger.R;
import com.jtjrenren.android.taxi.passenger.bean.api.ApiHistoryItem;
import com.jtjrenren.android.taxi.passenger.manger.AppManger;
import com.jtjrenren.android.taxi.passenger.manger.api.ApiWorks;
import com.jtjrenren.android.taxi.passenger.base.BaseActivity;
import com.jtjrenren.android.taxi.passenger.utils.InitView;
import com.jtjrenren.android.taxi.passenger.utils.SpaceItemDecoration;
import com.library.weidget.AutoLoadRecyclerView;


import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by wdl on 2016/1/22.
 */
public class OrderHistoryActivity extends BaseActivity {
    @BindView(R.id.order_history_refresh)
    SwipeRefreshLayout swp_refresh;
    @BindView(R.id.order_history_list)
    AutoLoadRecyclerView rcy_list;
    @BindView(R.id.order_histroy_rg)
    RadioGroup rg_con;
    @BindView(R.id.order_histroy_rb_all)
    RadioButton rb_all;
    @BindView(R.id.nocontent)
    LinearLayout ll_nonecontent;
    @BindView(R.id.title)
    TextView tv_title;

    private int page_size = 8;

    private List<ApiHistoryItem.DataEntity> orderList_all;
    private List<ApiHistoryItem.DataEntity> orderList_specar;
    private List<ApiHistoryItem.DataEntity> orderList_taxi;
    private int current_type = -1;
    private int page_index = 0;

    private boolean isRefreshing = false;

    private OrderHistoryAdapter adapter;

    private boolean isFristIN = true;

    @Override
    public int getLayoutId() {
        return R.layout.aty_order_history;
    }

    @Override
    public void init() {
        orderList_all = new ArrayList<>();
        orderList_specar = new ArrayList<>();
        orderList_taxi = new ArrayList<>();

        initViews();
    }

    private void initViews() {
        tv_title.setText(R.string.wodedingdan);


        InitView.initSwipeRefreshLayout(swp_refresh);
        InitView.initVritalReclyView(this, rcy_list);
        rcy_list.addItemDecoration(new SpaceItemDecoration(0, 0, 5, 0));

        adapter = new OrderHistoryAdapter(this);
        rcy_list.setAdapter(adapter);

        setListeners();

        rb_all.setChecked(true);
    }

    @Override
    protected void onResume() {
        super.onResume();

        if (!isFristIN) {
            isRefreshing = true;
            page_index = 0;
            getOrderHistory(true);
        } else
            isFristIN = false;
    }

    private void setListeners() {
        rcy_list.setLoadMoreListener(new AutoLoadRecyclerView.LoadMoreListener() {
            @Override
            public void loadMore() {
                loadMoreData();
            }
        });

        swp_refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isRefreshing = true;
                page_index = 0;
                getOrderHistory(false);
            }
        });
        rg_con.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                List<ApiHistoryItem.DataEntity> tmpList = orderList_all;
                switch (checkedId) {
                    case R.id.order_histroy_rb_all:
                        current_type = -1;
                        tmpList = orderList_all;
                        break;
                    case R.id.order_histroy_rb_specar:
                        current_type = Constants.CARTYPE_SPECAR;
                        tmpList = orderList_specar;
                        break;
                    case R.id.order_histroy_rb_taxi:
                        current_type = Constants.CARTYPE_TAXI;
                        tmpList = orderList_taxi;
                        break;
                }
                adapter.setList(tmpList);
                int length = tmpList.size();
                if (length == 0) {
                    page_index = 0;
                    getOrderHistory(true);
                } else {
                    page_index = length % page_size == 0 ? length / page_size : length / page_size + 1;
                }
            }
        });
        adapter.setListener(new OrderHistoryAdapter.itemClickListener() {
            @Override
            public void itemClick(ApiHistoryItem.DataEntity details) {
                Intent intent = new Intent(OrderHistoryActivity.this, OrderStatusActivity.class);
                intent.putExtra(OrderStatusActivity.EXTRA_ORDERSERNO, details.getID());
                startActivity(intent);
            }

            @Override
            public void payNow(ApiHistoryItem.DataEntity details) {
                startActivity(PayActivity.getDiyIntent(OrderHistoryActivity.this,details.getID()));

                finish();
            }
        });
    }

    /**
     * 加载更多的数据
     */
    private void loadMoreData() {
        getOrderHistory(false);
    }


    /**
     * 获取历史订单数据
     *
     * @param isShowWaitingDialog 是否展示加载按钮
     */
    private void getOrderHistory(boolean isShowWaitingDialog) {
        if (isShowWaitingDialog)
            showWaittingDialog(null, true);
        ApiWorks.getOrderHistory_M(AppManger.getInstance().user.getWorkNo(), page_size, page_index + 1, current_type,
                new ApiWorks.ResponseListener<ApiHistoryItem>() {
                    @Override
                    public void onResponse(ApiHistoryItem apiHistoryItem) {
                        if (swp_refresh.isRefreshing())
                            swp_refresh.setRefreshing(false);

                        if (rcy_list.isLoadingMore())
                            rcy_list.loadFinish(true);
                        if (apiHistoryItem != null && apiHistoryItem.getData() != null && apiHistoryItem.getData().size() > 0) {
                            List<ApiHistoryItem.DataEntity> data = apiHistoryItem.getData();
                            if (ll_nonecontent.getVisibility() == View.VISIBLE)
                                ll_nonecontent.setVisibility(View.GONE);
                            if (isRefreshing) {
                                isRefreshing = false;
                                adapter.replace(data);
                            } else {
                                adapter.appendList(data);
                            }
                            page_index++;

                            dismissWaittingDialog();
                            return;
                        }
                        getDataNoData();
                        dismissWaittingDialog();
                    }
                });
    }

    /**
     * 请求数据 返回的为空
     */
    private void getDataNoData() {
        if (page_index == 0)
            ll_nonecontent.setVisibility(View.VISIBLE);
    }


    @OnClick(R.id.title_left)
    public void clickBack(){
        finish();
    }
}
